/**
 *
 * Copyright 2015 RSVP Technologies Inc. All rights reserved.
 * TuLing.java
 *
 */
package com.gfinal.tuling;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 图灵机器人
 * 
 * @author Gao Yudong (gyd@nbicc.com)
 * @date 2015-3-9
 */
public abstract class TuLing {
	private String KEY = "b0e8c9094cfd82787c90f3fa8f798c94";
	private static final String URL = "http://www.tuling123.com/openapi/api?key=";

	private static Map<Integer, Class<?>> map = new HashMap<Integer, Class<?>>();
	static {
		map.put(100000, Text.class);
		map.put(200000, Link.class);
		map.put(302000, NEWS.class);
		map.put(304000, Download.class);
		map.put(305000, Train.class);
		map.put(306000, Flight.class);
		map.put(308000, Film.class);
		map.put(309000, Hotel.class);
		map.put(311000, Price.class);
	}
	
	public TuLing() {
		
	}

	public TuLing(String key) {
		this.KEY = key;
	}

	public void execute(User user) throws Exception {
		if (user == null || user.getInfo() == null) {
			throw new Exception("Invalid input");
		}

		String info = user.getInfo();
		String loc = user.getLoc();
		String lat = String.valueOf(user.getLat());
		String lon = String.valueOf(user.getLon());
		String userid = user.getUserId();
		String getURL = URL + KEY + "&info=" + info + "&loc=" + loc + "&lat="
				+ lat + "&lon=" + lon + "&userid=" + userid;

		java.net.URL getUrl = new java.net.URL(getURL);
		HttpURLConnection connection = (HttpURLConnection) getUrl
				.openConnection();
		connection.connect();
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				connection.getInputStream(), "utf-8"));
		StringBuffer sb = new StringBuffer();
		String line = "";
		while ((line = reader.readLine()) != null) {
			sb.append(line);
		}
		reader.close();
		connection.disconnect();

		JSONObject obj = JSON.parseObject(sb.toString());
		int code = obj.getIntValue("code");
		Object response = JSON.parseObject(sb.toString(), map.get(code));
		switch (code) {
			case 100000 :
				handleNormalType((Text) response);
				break;
			case 200000 :
				handleLinkType((Link) response);
				break;
			case 302000 :
				handleNewsType((NEWS) response);
				break;
			case 304000 :
				handleDownloadType((Download) response);
				break;
			case 305000 :
				handleTrainType((Train) response);
				break;
			case 306000 :
				handlerFlightType((Flight) response);
				break;
			case 308000 :
				handleFilmType((Film) response);
				break;
			case 309000 :
				handleHotelType((Hotel) response);
				break;
			case 311000 :
				handlePriceType((Price) response);
				break;
			default :
				handleError();
				break;
		}
	}
	
	/**
	 * 错误处理
	 */
	protected abstract void handleError();
	
	/**
	 * 处理一般文本回答
	 * @param obj
	 */
	protected abstract void handleNormalType(Text obj);
	
	/**
	 * 处理链接类回答
	 * @param obj
	 */
	protected abstract void handleLinkType(Link obj);
	
	/**
	 * 处理新闻类回答
	 * @param obj
	 */
	protected abstract void handleNewsType(NEWS obj);
	
	/**
	 * 处理下载类回答
	 * @param obj
	 */
	protected abstract void handleDownloadType(Download obj);
	
	/**
	 * 处理列车类回答
	 * @param obj
	 */
	protected abstract void handleTrainType(Train obj);
	
	/**
	 * 处理航班类回答
	 * @param obj
	 */
	protected abstract void handlerFlightType(Flight obj);
	
	/**
	 * 处理电影、小说、视频、菜谱类回答
	 * @param obj
	 */
	protected abstract void handleFilmType(Film obj);
	
	/**
	 * 处理酒店类回答
	 * @param obj
	 */
	protected abstract void handleHotelType(Hotel obj);
	
	/**
	 * 处理价格类回答
	 * @param obj
	 */
	protected abstract void handlePriceType(Price obj);

}
